package com.hse;

import com.hse.gui.ClientComponents.TempSensorGraphComponent;
import com.hse.gui.ClientComponents.TempSwitcherComponent;
import com.hse.gui.Form;
import com.hse.gui.IClientComponent;
import com.hse.network.ICoordinator;
import com.hse.network.ICoordinatorClient;
import com.hse.network.NetworkFactory;
import com.hse.network.exceptions.NetworkException;

import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: Nosov Alexander
 * Date: 30.05.2009
 * Time: 19:54:11
 * To change this template use File | Settings | File Templates.
 */
public class SensorAnalyser extends Thread {
    ICoordinator coordinator;
    ICoordinatorClient tempSensor;
    ICoordinatorClient tempSwitcher;

    IClientComponent tempSensorComponent;
    IClientComponent tempSwitcherComponent;

    private boolean started = false;

    Form form;

    public SensorAnalyser(Form form) {
        this.form = form;
    }

    public void initCoordinator(String port) throws InstantiationException {
        coordinator = NetworkFactory.createCoordinator(port, 0);
    }

    public void closeCoordinator() {
        coordinator.close();
    }

    public void createNetwork() throws NetworkException {
        short panId = (short) 12345;
        coordinator.createNetwork(panId);
        started = true;
    }

    public void destroyNetwork() throws NetworkException {
        started = false;
        coordinator.destroyNetwork();
        tempSensor = null;
        tempSwitcher = null;
        tempSensorComponent = null;
        tempSwitcherComponent = null;
        form.removeClients();
    }

    public void acceptClients() throws NetworkException {
        if ((tempSensor == null || tempSwitcher == null) && coordinator.canAccept()) {
            ICoordinatorClient client = coordinator.accept();
            byte[] data = client.recv();
            String typename = new String(data);
            String[] typenamearr = typename.split(";", 2);
            if (typenamearr[0].compareTo("t") == 0) {
                tempSensor = client;
                tempSensorComponent = new TempSensorGraphComponent();
                form.addClient(tempSensorComponent);
                form.log("Temperature sensor connected");
            } else {
                tempSwitcher = client;
                tempSwitcherComponent = new TempSwitcherComponent();
                form.addClient(tempSwitcherComponent);
                form.log("Temperature switcher connected");
            }
        }
    }

    public void sendRecieveData() throws NetworkException {
        byte[] recv = null;
        //recieve|send info

        //if tempSensor exists
        if (tempSensor != null && tempSensor.canRead()) {
            //recieve data from tempSensor device
            recv = tempSensor.recv();
            //send data to tempSensorComponent
            tempSensorComponent.recv(recv);
            form.log("Data recieved from temperature sensor: " + new String(recv));


        }

        //if tempSwitcher device exist
        if (tempSwitcher != null) {
            //send data from tempSensor to tempSwitcherComponent
            if (recv != null) {
                tempSwitcherComponent.recv(recv);
            }
            //recieve new data(on or off) from tempSwitcherComponent
            byte[] send = tempSwitcherComponent.send();
            //send new data to tempSwitcher
            if (send != null) {
                tempSwitcher.send(send);
                form.log("Data sended to temperature switcher: " + new String(send));
            }
        }
    }

    public void run1() {
        int i = 13;
        while (true) {
            if (started) {
                //ccept new clients
                //if(coordinator.isNewClientsAvailable)

                //accepting clients
                //ICoordinatorClient client = coordinator.accept();
                if (tempSensorComponent == null) {
                    tempSensorComponent = new TempSensorGraphComponent();
                    form.addClient(tempSensorComponent);
                }
                if (tempSwitcherComponent == null) {
                    tempSwitcherComponent = new TempSwitcherComponent();
                    form.addClient(tempSwitcherComponent);
                }

                byte[] recv = null;
                //recieve data from tempSensor device
                recv = String.valueOf(new Random().nextInt(40) - 20).getBytes();

                //send data to tempSensorComponent
                tempSensorComponent.recv(recv);

                //send data from tempSensor to tempSwitcherComponent
                if (recv != null) {
                    tempSwitcherComponent.recv(recv);
                }
                //recieve new data(on or off) from tempSwitcherComponent
                byte[] send = tempSwitcherComponent.send();

                String cendDtring = new String(send);
                boolean switchON = Boolean.valueOf(cendDtring);

                i++;
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }


            }

        }
    }

    public void run() {
        while (true) {
            if (started) {
                //ccept new clients
                //if(coordinator.isNewClientsAvailable)

                try {
                    //accepting clients
                    acceptClients();
                    sendRecieveData();

                } catch (NetworkException e) {
                    form.showErrorMessage(e.getLocalizedMessage());
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }


            }

        }
    }
}
